package com.QR;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.Buffer;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-09-07
 * Time:23:39
 * Text：
 */
@Component
public class QRCodeUtils {
    public void createQRCode(String filePath,String content){
        int width = 300;
        int height = 300;
        String format = "png";
        /*
         * 定义二维码的参数
        **/
        HashMap hints=new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);  //纠错等级为2
        hints.put(EncodeHintType.MARGIN,2);  //图片的边距

        /*
            生成二维码
        */
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height);
            Path file = new File(filePath).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix,format,file);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public String getContentFromQRCode(String filePath){
        MultiFormatReader formatReader = new MultiFormatReader();
        File file = new File(filePath);
        BufferedImage image;
        try{
            image = ImageIO.read(file);
            BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer
                    (new BufferedImageLuminanceSource(image)));
            HashMap hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
            Result result = formatReader.decode(binaryBitmap,hints);
            return result.toString();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        new QRCodeUtils().createQRCode("D:\\Code\\java\\project\\QR\\image\\one.png","123456");

    }
}
